package com.itest.controller;

import com.itest.entity.User;
import com.itest.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/*
 * 赵金龙 魏巍
 * */

@RestController
public class Register {

    @Autowired
    private UserMapper userMapper;

    @CrossOrigin
    @PostMapping(value="/register",consumes = "application/json;charset=UTF-8")
    public String register(@RequestBody User resister_user,HttpServletResponse response) {

        String retusername=resister_user.getUsername();
        String retpassword=resister_user.getPassword();
        String renpassword=resister_user.getRpassword();

        User user0 = new User();
        user0.setUsername(retusername);
        user0.setPassword(retpassword);

        if(userMapper.findUser(resister_user)!=null){
            System.out.println("用户"+retusername+"已存在！");
            return "用户"+retusername+"已存在！";
        }else if(retusername.length()>16 || retusername.length()<6 ){
            System.out.println("用户名长度不符合要求！");
            return "用户名长度不符合要求！";
        }else if(retpassword.length()>16 || retpassword.length()<6){
            System.out.println("密码长度不符合要求！");
            return"密码长度不符合要求!";
        }else if(!retpassword.equals(renpassword)){
            return "新密码与确认新密码不一致";
        }else{
            userMapper.insertUser(user0);
            System.out.println(retusername+"注册成功！");
            return retusername+"注册成功！";
        }
    }
}
